package com.jinniulive.platform.module.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.ModifyPersonalInfo;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.common.util.MD5;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class PasswordSettingActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener {

    private EditText mEditTextOld,mEditTextNew,mEditTextAgain;
    private TextView mTextViewForget;
    private Button mButtonOk;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_setting);
        initView();
        setListener();

    }

    private void setListener() {
        mEditTextOld.addTextChangedListener(this);
        mEditTextNew.addTextChangedListener(this);
        mEditTextAgain.addTextChangedListener(this);
        mTextViewForget.setOnClickListener(this);
    }

    private void initView() {
        mEditTextOld = (EditText) findViewById(R.id.activity_password_setting_et_old);
        mEditTextNew = (EditText) findViewById(R.id.activity_password_setting_et_new);
        mEditTextAgain = (EditText) findViewById(R.id.activity_password_setting_et_again);
        mTextViewForget = (TextView) findViewById(R.id.activity_password_setting_tv_forget);
        mButtonOk = (Button) findViewById(R.id.activity_password_setting_bt_ok);
    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()){
                case R.id.activity_password_setting_bt_back:
                    finish();
                    break;
                //忘记当前密码
                case R.id.activity_password_setting_tv_forget:
                    startActivity(new Intent(this,ForgetPasswordActivity.class));
                    finish();
                    break;
                case R.id.activity_password_setting_bt_ok:
                    Intent intent = getIntent();
                    String tokenID = intent.getStringExtra("tokenID");
                    String userID = intent.getStringExtra("userID");
                    String password = intent.getStringExtra("password");
                    String trimOld = mEditTextOld.getText().toString().trim();
                    final String trimNew = mEditTextNew.getText().toString().trim();
                    String trimAgain = mEditTextAgain.getText().toString().trim();
                    if (trimOld.equals(password)){
                        if (trimNew.length()>=6 && isLetterOrDigit(trimNew)){
                            if (trimAgain.equals(trimNew)){
                                String md5 = MD5.md5(trimNew);
                                String format = String.format(NetConfig.XGMM, tokenID, userID, md5);
                                RequestParams params=new RequestParams(format);
                                x.http().get(params, new Callback.CommonCallback<String>() {
                                    @Override
                                    public void onSuccess(String result) {
                                        Gson gson =new Gson();
                                        ModifyPersonalInfo info = gson.fromJson(result, ModifyPersonalInfo.class);
                                        int errorcode = info.getErrorcode();
                                        String codedes = info.getCodedes();
                                        if (errorcode== 200){
                                            SharedPreferences sp = getSharedPreferences("autoLogin", MODE_PRIVATE);
                                            SharedPreferences.Editor edit=sp.edit();
                                            edit.putString("password",trimNew);
                                            edit.commit();
                                            Toast.makeText(PasswordSettingActivity.this, "修改成功", Toast.LENGTH_SHORT).show();

                                        }else{
                                            Toast.makeText(PasswordSettingActivity.this, codedes, Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable ex, boolean isOnCallback) {

                                    }

                                    @Override
                                    public void onCancelled(CancelledException cex) {

                                    }

                                    @Override
                                    public void onFinished() {

                                    }
                                });

                            }else{
                                Toast.makeText(this, "俩次输入的密码不同", Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            Toast.makeText(this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                        }

                    }else {
                        Toast.makeText(this, "原密码不正确", Toast.LENGTH_SHORT).show();
                    }



                    break;


            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (TextUtils.isEmpty(mEditTextOld.getText().toString().trim())){
            mEditTextNew.setEnabled(false);
        }else{
            mEditTextNew.setEnabled(true);
        }
        if (TextUtils.isEmpty(mEditTextNew.getText().toString().trim())){
            mEditTextAgain.setEnabled(false);
        }else{
            mEditTextAgain.setEnabled(true);
        }
        if (TextUtils.isEmpty(mEditTextNew.getText().toString().trim())){
            mEditTextAgain.setEnabled(false);
        }else{
            mEditTextAgain.setEnabled(true);
        }
        if (TextUtils.isEmpty(mEditTextAgain.getText().toString().trim())){
            mButtonOk.setEnabled(false);
            mButtonOk.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
        }else{
            mButtonOk.setEnabled(true);
            mButtonOk.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_14));
        }


    }

    @Override
    public void afterTextChanged(Editable s) {

    }
    //判断字符串是否是 数字或字母
    public static boolean isLetterOrDigit(String str) {
        boolean isLetterOrDigit = false;//定义一个boolean值，用来表示是否包含字母或数字
        for (int i = 0; i < str.length(); i++) {
            if (Character.isLetterOrDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符
                isLetterOrDigit = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isLetterOrDigit && str.matches(regex);
        return isRight;
    }
}
